import { getSiteData, getTempSiteData } from '@/service/site'
import { EVENTS_RESULTS_TYPES_MAP, PAGE_TYPE } from '@/common/const'
import { GENERAL_TYPES_MAP } from '@/common/const-general'
import { copySiteData } from '@/common/site-data'
import { initComponentMapInPage } from '@/store/index-utils'
import { Toast } from 'vant'
import { EventBus } from '@/store'
import { H5_TYPE } from '@/common/const-dif'
import { Dialog } from 'vant'

export default {
  async updateTempSiteData({ commit, state, dispatch }, params){
    // Toast.loading({ message: '加载中...', forbidClick: true });
    let siteData =  null
    if(state.frontData.isLocalSiteData){
      state.siteInfo.code = 0
      siteData = copySiteData
      initComponentMapInPage(state.componentMap, siteData.pages)
      commit('setPageData', siteData)
      state.siteInfo.isLoading = false;
      state.siteInfo.isDisplayAssetsLoading = false;
      Toast.clear()
    }else{
      const { data } = await getTempSiteData(params)
      siteData = data.data
      state.siteInfo.msg = data.msg
      state.siteInfo.code = data.code
      if(data.code > 0){
        Dialog.alert({
          title: '',
          message: state.siteInfo.msg,
        })
        return
      }
      initComponentMapInPage(state.componentMap, siteData.pages)
      if(siteData.activityInfo && siteData.activityInfo.isHave){
        dispatch("activity/getSiteActivityData", siteData);
      }else{
        commit('setPageData', siteData)
      }
      // Toast.clear()
    }
  },
  async updateSiteData({ commit, state, dispatch }, params) {
    // Toast.loading({ message: '加载中...', forbidClick: true });
    const { data } = await getSiteData(params)
    let siteData = data.data
    state.siteInfo.code = data.code
    state.siteInfo.msg = data.msg
    if(data.code > 0) {
      Dialog.alert({
        title: '',
        message: state.siteInfo.msg,
      })
      
      return
    }
    const jumpUrl = siteData.attrs.offlineJumpLink
    if(siteData.attrs.isOffline && jumpUrl) {
      window.location.href = jumpUrl
      return
    }
    initComponentMapInPage(state.componentMap, siteData.pages)
    if(siteData.activityInfo && siteData.activityInfo.isHave){
      dispatch("activity/getSiteActivityData", siteData)
    }else{
      commit('setPageData', siteData)
    }
    // Toast.clear()
  },
  updateLoginStatus: ({commit}, isLogin) => {
    commit('updateLoginStatus', isLogin)
  },
  updateInitGeneral: ({commit}, params) => {
    commit('updateInitGeneral', params)
  },
  updateActivityInfo: ({commit}, data) => {
    commit('updateActivityInfo', data)
  },
  updateControls: ({commit}, data) => {
    commit('updateControls', data);
  },
  updateComponentAttr: ({commit}, data) => {
    commit('updateComponentAttr', data);
  },
  updateArea({commit, state}, data){
    commit('updateArea', data);
    let { type } = data;
    if(type == GENERAL_TYPES_MAP.area){
      if(!data.data.area_code || !data.data.area[data.data.area_code]){
        let eventAttr = state.pages[0].eventAttr || state.pages[1].eventAttr;

        if(eventAttr.resultType == EVENTS_RESULTS_TYPES_MAP.page) {
          // 触发事件后 跳转页面
          let pageId = eventAttr.failPanelId;
          if(pageId){
            commit('updatePageIndex', { pageIndex: pageId - 1 });
            EventBus.$emit("showPage", {pageId})
          }
        } else if (eventAttr.resultType == EVENTS_RESULTS_TYPES_MAP.pop) {
          let popId = eventAttr.failPanelId;
          if(popId){
            commit('updatePopId', { popId });
            EventBus.$emit("showPop", {popId})
          }
        }
      }
    }
  },
  setSitePages({commit, state}, data){
    commit('setSitePages', data);
    //打开页面 弹层
    let pageId = state.attrs.pageId;
    let popId = state.attrs.popId;
    if(pageId > 0){
      commit('updatePageIndex', { pageIndex: pageId - 1 }, {root:true});
  
      EventBus.$emit('pauseVideo', { pageId })
      EventBus.$emit('playOrPauseAudio', { isPlay: false })
      if (state.attrs.h5Type == H5_TYPE.mobile && !state.attrs.isNoSwiping && state.attrs.pageType == PAGE_TYPE.single) {
        EventBus.$emit("swiper", { pageId })
      } else {
        EventBus.$emit('showPage', { pageId })
      }
    }
    if(popId > 0){
      commit('updatePopId', { popId }, {root:true});
      EventBus.$emit("showPop", {popId});
    }
  }
}
